{include file="public/header296"}
<div class="layui-fluid">
  <div class="layui-card">
    <div class="layui-form layui-card-header layuiadmin-card-header-auto" style="padding-top: 10px;">
      <div class="layui-form-item">
        <div class="layui-inline">
          角色筛选
        </div>
        <div class="layui-inline">
          <select name="rolename" lay-filter="LAY-user-adminrole-type">
            <option value="0">全部角色</option>
            {foreach $role_items as $item}
              <option value="{$item.id}">{$item.name}</option>
            {/foreach}
          </select>
        </div>
      </div>
    </div>
    <div class="layui-card-body">
      <div style="padding-bottom: 10px;">
        <button class="layui-btn layuiadmin-btn-list" data-type="insert">添加</button>
      </div>
      <table id="LAY-app-content-list" lay-filter="LAY-app-content-list"></table>
      <script type="text/html" id="table-content-list">
        {{#  if(d.id != 1){ }}
        <a class="layui-btn layui-btn-xs " lay-event="view"><i class="layui-icon"></i>权限</a>
        {{# } }}
        <a class="layui-btn layui-btn-xs layui-btn-normal " lay-event="edit"><i class="layui-icon"></i>编辑</a>
        {{#  if(d.id == 1){ }}
        <a class="layui-btn layui-btn-xs layui-btn-disabled "><i class="layui-icon"></i>删除</a>
        {{# } else{ }}
        <a class="layui-btn layui-btn-xs layui-btn-danger " lay-event="delete"><i class="layui-icon"></i>删除</a>
        {{# } }}
      </script>
    </div>
  </div>
</div>
<style>
  .layui-table .layui-table-cell{
    height: auto;
    line-height: 44px;
    max-width: 100%;
  }
</style>
<script>
  layui.use(['layer', 'table'], function () {
    var $ = layui.$
            , table = layui.table
            , form = layui.form;
    // 搜索角色
    form.on('select(LAY-user-adminrole-type)', function(data){
      table.reload('LAY-app-content-list', {
        where: {
          role_id: data.value
        }
      });
    });
    table.render({
      elem: '#LAY-app-content-list'
      , url: "{:url('admin/getList')}"
      , request: {
        pageName: 'page',
        limitName: 'page_size'
      }
      , page: {
        layout: ['limit', 'count', 'prev', 'page', 'next', 'skip']
        ,first: '首页'
        ,last: '尾页'
        ,limit: 10
      }
      , toolbar: true
      , totalRow: true
      , cellMinWidth: 120
      , cols: [[
        {field: 'id', type: 'checkbox', fixed: 'left', title: '全选'}
        , {field: 'id', width: 80, title: 'ID', align: 'center', sort: true}
        ,{field: 'avatar', title: "头像", width: 45, align:'center', templet: function (d) {
          if (d.avatar != '') {
            str = '<div id="layer-photos-cover-'+ d.id +'" class="layer-photos-demo">';
            str +='<img layer-src="'+ d.avatar +'" style="width:40px;height: 40px" src="' + d.avatar + '">';
            return str;
          } else{
            return '';
          }
        }}
        , {field: 'name', width: 100, title: '角色', align: 'center'}
        , {field: 'username', title: '用户名', align:'center', minWidth: 100}
        , {field: 'nickname', title: '昵称', align:'center', minWidth: 120}
        , {field: 'phone', title: '手机', align:'center', minWidth: 150}
        , {field: 'update_time', title: '最后更新时间', minWidth: 150, align:'center'}
        , {field: 'login_time', title: '最后登录', minWidth: 150, align:'center', templet: function (d) {
            if (d.login_time) {
              return d.login_time;
            } else {
              return '';
            }
        }}
        , {title: '操作', width: 210, align: 'center', fixed: 'right', toolbar: '#table-content-list'}
      ]]
      , text: {
        none: '暂无相关数据'
      }
      , done: function () {
        hoverOpenImage();
      },
    });
    //监听工具条，编辑（查看），解绑，已审核
    table.on('tool(LAY-app-content-list)', function (obj) {
      var data = obj.data;
      var id = data.id;
      if (obj.event === 'edit') {
        layer.open({
          type: 2
          , title: '编辑'
          , content: "{:url('admin/edit')}?id=" + data.id
          , maxmin: true
          , area: ['800px', '85%']
          ,btn: ['提交', '取消']
          , yes: function (index, layero) {
            var iframeWindow = window['layui-layer-iframe'+ index]
                    ,submit = layero.find('iframe').contents().find("#Lay-app-form-edit");

            iframeWindow.layui.form.on('submit(Lay-app-form-edit)', function(data){
              var field = data.field;
              $.ajax({
                url:"{:url('admin/edit')}",
                type:'post',
                data:{'field':field},
                success:function(res){
                  if(res.code == 0){
                    layer.msg(res.msg,{icon:1});
                    layer.close(index);
                    table.reload('LAY-app-content-list');
                  } else if (res.code == 1){
                    return layer.msg(res.msg,{icon:5});
                  }
                }
              });
            });
            submit.trigger('click');
          }
        });
      } else if (obj.event === 'delete') {
        layer.confirm('确定删除该用户吗，请谨慎操作？', {title : '温馨提示'}, function(index){
          $.ajax({
            url:"{:url('admin/delete')}",
            type:'post',
            data:{'id':id},
            success: function(res){
              if(res.code == 0){
                obj.del();
                layer.msg(res.msg,{icon:1});
              } else {
                layer.msg(res.msg,{icon:5});
              }
            }
          });
        });
      } else if (obj.event === 'view') {
        layer.open({
          type: 2
          , title: '编辑'
          , content: "{:url('admin/view')}?role_id=" + data.role_id
          , maxmin: true
          , area: ['800px', '85%']
          ,btn: ['关闭']
          , yes: function () {
            layer.closeAll();
          }
        });
      }
    });

    form.on('submit(LAY-app-contlist-search)', function (data) {
      var field = data.field;
      table.reload('LAY-app-content-list', {
        page: {
          curr: 1
        }
        , where: field
      });
    });
    // 监听开关操作
    form.on('switch(status)', function(obj){
      if (obj.elem.checked == true){
        status = 1;
      } else if(obj.elem.checked == false){
        status = 0;
      }
      var id = this.value;
      $.post("{:url('user/updateStatus')}", {id:id, status:status}, function (res) {
        if (res.code == 0) {
          layer.msg(res.msg, {icon: 1, time: 1500});
        } else {
          layer.msg(res.msg, {icon: 5, time: 2000});
        }
      });
    });

    function hoverOpenImage(){
      var img_show = null;
      $('td img').hover(function(){
        var img = "<img class='img_msg' src='"+$(this).attr('src')+"' style='width:275px;' />";
        img_show = layer.tips(img, this,{
          tips:[1, '#000']
          , area: ['300px', '290px']
          , maxWidth: 300
        });
      },function(){
        layer.close(img_show);
      });
    }

    var $ = layui.$, active = {
      insert: function(){
        layer.open({
          type: 2
          ,title: '添加管理员'
          ,content: "{:url('admin/insert')}"
          ,maxmin: true
          ,area: ['800px', '85%']
          ,btn: ['确定', '取消']
          ,yes: function(index, layero){
            var iframeWindow = window['layui-layer-iframe'+ index]
                    ,submit = layero.find('iframe').contents().find("#Lay-app-form-submit");

            iframeWindow.layui.form.on('submit(Lay-app-form-submit)', function(data){
              var field = data.field;
              $.ajax({
                url:"{:url('admin/insert')}",
                type:'post',
                data:{'field':field},
                success:function(res){
                  if(res.code == 0){
                    layer.msg(res.msg,{icon:1});
                    table.reload('LAY-app-content-list');
                    layer.close(index);
                  } else{
                    return layer.msg(res.msg,{icon:5});
                  }
                }
              });
            });
            submit.trigger('click');
          }
        });
      }
    };

    $('.layui-btn.layuiadmin-btn-list').on('click', function () {
      var type = $(this).data('type');
      active[type] ? active[type].call(this) : '';
    });
  });
</script>
{include file="public/footer" /}